package tech.anzhen.simple.rpc.support.rest;

import java.lang.reflect.Proxy;

public class RestProxyFactory {

    private RestClient restClient;

    private String baseAddress;

    public RestProxyFactory(RestClient restClient, String baseAddress) {
        this.restClient = restClient;
        this.baseAddress = baseAddress;
    }

    @SuppressWarnings("unchecked")
    public <T> T getProxy(Class<T> serviceInterface) {
        String url = baseAddress;

        url = RestUtils.pathAppend(url, serviceInterface.getSimpleName());

        if (!url.endsWith("/")) {
            url = url + "/";
        }

        RestClientInvocationHandler h = new RestClientInvocationHandler(restClient, url);
        Object proxy = Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
                new Class[]{serviceInterface}, h);
        return (T) proxy;
    }
}
